package com.tannm.doan.taskmanager.agenda;

import java.util.*;

import com.tannm.doan.taskmanager.TaskManager;
import com.tannm.doan.taskmanager.dataview.DataView;
import com.tannm.doan.taskmanager.dataview.DataViewItem;
import com.tannm.doan.taskmanager.views.ViewTodayItemAppointment;
import com.tannm.doan.taskmanager.views.ViewTodayItemHeader;
import com.tannm.doan.taskmanager.views.ViewTodayItemNote;
import com.tannm.doan.taskmanager.views.ViewTodayItemTask;

import com.tannm.doan.taskmanager.R;
import android.view.View;
import android.widget.LinearLayout;
import android.graphics.*;

public class AgendaViewToday extends AgendaView {
	// fields
	private Vector<ViewTodayItemHeader> tdhitems = new Vector<ViewTodayItemHeader>();

	// fields
	private final int iTopPadding = 2;
	private final int iBottomPadding = 2;
	private final int iHeadAppts = 0;
	private final int iHeadTasks = 1;
	private final int iHeadNotes = 2;

	// fields
	private int iSpaceWidthTime = 0;
	private int iSpaceWidthMinutes = 0;
	private int iSpaceWidthUSTimeMark = 0;

	// fields
	private Paint mpt = new Paint();

	// methods
	public AgendaViewToday(TaskManager main) {
		super(main);
		iSpaceWidthTime = ViewTodayItemAppointment.getSpaceWidthTime(mpt);
		iSpaceWidthMinutes = ViewTodayItemAppointment.getSpaceWidthMinutes(mpt);
		iSpaceWidthUSTimeMark = ViewTodayItemAppointment
				.getSpaceWidthUSTimeMark(mpt);
	}

	@Override
	public int getViewType() {
		return viewMode.TODAY;
	}

	@Override
	public int getViewIndex() {
		return 0;
	}

	@Override
	public void rebuild() {
		llayParent.removeAllViews();

		ViewTodayItemHeader tdhi = null;

		// create today item headers
		for (int i = 0; i < 3; i++)
			tdhitems.add(new ViewTodayItemHeader(main));

		// init appointments
		tdhi = initHeaderItem(iHeadAppts,
				ViewTodayItemHeader.ViewType.Appointments,
				R.string.labTodayItemAppointments);
		llayParentAppt.addView(tdhi, lparams);

		// init tasks
		tdhi = initHeaderItem(iHeadTasks, ViewTodayItemHeader.ViewType.Tasks,
				R.string.labTodayItemTasks);
		llayParentTask.addView(tdhi, lparams);

		// init notes
		tdhi = initHeaderItem(iHeadNotes, ViewTodayItemHeader.ViewType.Notes,
				R.string.labTodayItemNotes);
		llayParentNote.addView(tdhi, lparams);

		llayParent.addView(llayParentAppt, lparams);
		llayParent.addView(llayParentTask, lparams);
		llayParent.addView(llayParentNote, lparams);
	}

	public ViewTodayItemHeader initHeaderItem(int index,
			ViewTodayItemHeader.ViewType type, int iResStrId) {
		ViewTodayItemHeader tdhi = null;
		// init appointments
		tdhi = tdhitems.get(index);
		tdhi.setType(type);
		tdhi.setText(main.utils.getResStr(iResStrId));
		tdhi.setInfoText(sTextNone);
		tdhi.setPadding(0, iTopPadding, 0, iBottomPadding);
		// set event
		tdhi.setItemClick(new ViewTodayItemHeader.OnHeaderItemClick() {
			public void OnClick(View v, ViewTodayItemHeader.ViewType type) {
				doHeaderItemClick(v, type);
			}
		});
		return tdhi;
	}

	public void removeChildViewsFromHeader(LinearLayout llay) {
		while (llay.getChildCount() > 1) {
			View v = llay.getChildAt(1);
			if (v == null)
				break;
			if (v.getClass() != ViewTodayItemHeader.class)
				llay.removeViewInLayout(v);
		}
		llayParent.invalidate();
	}

	public void updateInfoText(ViewTodayItemHeader tdhi, int iRowsCount) {
		if (iRowsCount == 0) {
			tdhi.setInfoText(sTextNone);
		} else {
			tdhi.setInfoText(Integer.toString(iRowsCount));
		}
	}

	@Override
	public void rebuildViewAppointments(DataView dataView) {
		removeChildViewsFromHeader(llayParentAppt);

		for (int i = 0; i < dataView.getRowsCountTotal(); i++) {
			DataViewItem row = dataView.getRow(i, this.getViewType());
			if (row != null) {
				ViewTodayItemAppointment item = new ViewTodayItemAppointment(
						main);
				item.setRowId(row.lID);
				item.setItemTime(row.getStartHour(), row.getStartMinute(),
						false, main.prefs.b24HourMode, iSpaceWidthTime,
						iSpaceWidthMinutes, iSpaceWidthUSTimeMark);
				item.setItemData(row.sSubject, row.bAlarm, row.isRepeat());
				item.setItemClick(onApptItemClick);
				llayParentAppt.addView(item, lparams);
			}
		}
		updateInfoText(tdhitems.get(iHeadAppts), dataView
				.getRowsCountForView(this.getViewType()));
	}

	@Override
	public void rebuildViewTasks(DataView dataView) {
		removeChildViewsFromHeader(llayParentTask);

		for (int i = 0; i < dataView.getRowsCountTotal(); i++) {
			DataViewItem row = dataView.getRow(i, this.getViewType());
			if (row != null) {
				ViewTodayItemTask item = new ViewTodayItemTask(main);
				item.setRowId(row.lID);
				item.setItemData(row.bDone, row.sSubject, row.bAlarm);
				item.setItemClick(onTaskItemClick);
				llayParentTask.addView(item, lparams);
			}
		}
		updateInfoText(tdhitems.get(iHeadTasks), dataView
				.getRowsCountForView(this.getViewType()));
	}

	@Override
	public void rebuildViewNotes(DataView dataView) {
		removeChildViewsFromHeader(llayParentNote);

		for (int i = 0; i < dataView.getRowsCountTotal(); i++) {
			DataViewItem row = dataView.getRow(i, this.getViewType());
			if (row != null) {
				ViewTodayItemNote item = new ViewTodayItemNote(main);
				item.setRowId(row.lID);
				item.setItemData(row.sSubject);
				item.setItemClick(onNoteItemClick);
				llayParentNote.addView(item, lparams);
			}
		}
		updateInfoText(tdhitems.get(iHeadNotes), dataView
				.getRowsCountForView(this.getViewType()));
	}

	@Override
	public void updateTimeFormat() {
	}

}
